package com.acme.bankapp.service.bank;

import java.io.FileNotFoundException;
import java.io.IOException;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.domain.exceptions.BankException;
import com.acme.bankapp.domain.exceptions.InvalidFeedDataException;
import com.acme.bankapp.domain.exceptions.NotEnoughFundsException;
import com.acme.bankapp.domain.exceptions.OverdraftLimitExceededException;


public class BankApplication {
	private static String LOAD_FEED_FLAG = "-loadfeed";
	private static String LOAD_BANK_FLAG = "-loadbank";
	private static String RUN_SERVER_FLAG = "-server";
	
	public static void main(String[] args) {
		BankService bankService = new BankService();
		Bank bank = null;
		if (args.length >= 2 && LOAD_BANK_FLAG.equals(args[0])) {
			try {
				bank = bankService.readBank(args[1]);
			} catch (BankException e) {
				e.printStackTrace();
			}
		} else {
			bankService.createBank();
		}
		if (args.length >= 2 && LOAD_FEED_FLAG.equals(args[0])) {
			BankDataLoaderService dataLoaderService = new BankDataLoaderService();
			try {
				dataLoaderService.readBank(bank, args[1]);
			} catch (InvalidFeedDataException e) {
				System.out.println("Invalid feed file");
			} catch (FileNotFoundException e) {
				System.out.println("Feed file not found");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (args.length > 2 && RUN_SERVER_FLAG.equals(args[2])) {
			
		}
		bankService.printMaximumAmountToWithdraw(bank);
	}
	
	private static void modifyBank(Bank bank) {
		bank.getClient(1).getAccount().deposit(10.0);
		try {
			bank.getClient(2).getAccount().withdraw(14.0);
		} catch (OverdraftLimitExceededException e) {
			System.out.println(e.getMessage());
		} catch (NotEnoughFundsException e) {
			System.out.println(e.getMessage());
		}
	}
	
	private static void printBalance(Bank bank) {
		for (Client client : bank.getClients()) {
			System.out.println(client);
		}
	}
}
